#pragma once

#include <iostream>
#include "Log.hpp"
#include "Connection.hpp"
#include "Epoller.hpp"
#include "Common.hpp"
#include <unistd.h>
#include <unordered_map>
#include <memory>


class TcpServer
{
public:
    TcpServer():_epoller_ptr(std::make_unique<Epoller>())
    {
    }

    void Start()
    {
        while(true)
        {
            _epoller_ptr->Wait();
        }
    }

    void AddConnection(std::shared_ptr<Connection> Con)
    {
        //将Con对应的fd以及关心的事件写入到内核中
        int events = Con->GetEvent();
        int sockfd = Con->GetFd();
        _epoller_ptr->AddEvent(sockfd,events);
        _connection[sockfd] = Con;
    }

    ~TcpServer()
    {}
private:
    //epoll模型
    std::unique_ptr<Epoller> _epoller_ptr;
    //管理对应的fd
    std::unordered_map<int,std::shared_ptr<Connection>> _connection;
};